package com.zmn.plat.services.impl.brand;

import java.util.List;

import javax.annotation.Resource;

import com.zmn.common.dto.VtDTO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zmn.plat.manager.interfaces.cache.CommonCache;
import com.zmn.plat.model.entity.brand.BaseBrand;
import com.zmn.plat.model.entity.brand.BaseBrandQuery;
import com.zmn.plat.model.entity.brand.BaseBrandTariffProductQuery;
import com.zmn.plat.model.vo.brand.factor.BrandFactorRatioVo;
import com.zmn.plat.persistence.interfaces.brand.BaseBrandDao;
import com.zmn.plat.services.interfaces.brand.BaseBrandService;

/**
 * @author duanzuocai
 * @version v1.0
 * @since 2018/11/8 9:42
 */
@Service
public class BaseBrandServiceImpl implements BaseBrandService {

    @Resource
    BaseBrandDao baseBrandDao;
    @Resource
    CommonCache commonCache;

    @Override
    public BaseBrand findByKey(Integer brandId) {
        return baseBrandDao.findByKey(brandId);
    }

    @Override
    public List<BaseBrand> listPageByQuery(BaseBrandQuery query) {
        int count = countByQuery(query);
        query.setTotalCount(count);
        return baseBrandDao.listPageByQuery(query);
    }

    @Override
    public List<VtDTO> listByServCategIdAndCategOneId(Integer servCategId, Integer categOneId) {
        return baseBrandDao.listByServCategIdAndCategOneId(servCategId, categOneId);
    }

    @Override
    public List<BrandFactorRatioVo> listTariffBrandPageByQuery(BaseBrandTariffProductQuery query) {
        // int count = baseBrandDao.countTariffBrandPageByQuery(query);
        // query.setTotalCount(count);
        return baseBrandDao.listTariffBrandPageByQuery(query);
    }

    @Override
    public List<BrandFactorRatioVo> listTariffChannelBrandPageByQuery(BaseBrandTariffProductQuery query) {
        // int count = baseBrandDao.countTariffChannelBrandPageByQuery(query);
        // query.setTotalCount(count);
        return baseBrandDao.listTariffChannelBrandPageByQuery(query);
    }

    @Override
    public List<BaseBrand> listUnlinkedBrandPageByQuery(BaseBrandQuery query) {
        int count = baseBrandDao.countUnlinkedBrandPageByQuery(query);
        query.setTotalCount(count);
        return baseBrandDao.listUnlinkedBrandPageByQuery(query);
    }

    @Override
    public List<BaseBrand> listByQuery(BaseBrandQuery query) {
        return baseBrandDao.listByQuery(query);
    }

    @Override
    public List<BaseBrand> listAll() {
        return baseBrandDao.listAll();
    }

    @Override
    public Integer countByQuery(BaseBrandQuery query) {
        return baseBrandDao.countByQuery(query);
    }

    @Override
    public Integer updateStatusByKey(BaseBrand brand) {
        clearCacheByBrandId(brand.getBrandId());
        clearCacheAll();
        return baseBrandDao.updateStatusByKey(brand);
    }

    @Override
    public Integer updateStatusBatchByKey(List<Integer> ids, Integer status, String mcStaffName) {
        clearCacheAll();
        if (!CollectionUtils.isEmpty(ids)) {
            ids.forEach(e -> clearCacheByBrandId(e));
        }
        return baseBrandDao.updateBatchStatusByKey(ids, status, mcStaffName);
    }

    @Override
    public Integer updateSortByKey(BaseBrand brand) {
        return baseBrandDao.updateSortByKey(brand);
    }

    @Override
    public Integer updateSortByKey(Integer brandId, Integer sort, String mcStaffName) {
        BaseBrand brand = new BaseBrand();
        brand.setBrandId(brandId);
        brand.setSort(sort);
        brand.setUpdater(mcStaffName);
        return updateSortByKey(brand);
    }

    @Override
    @CacheEvict(cacheNames = "redis1d", key = "'brand:id:'+#brand.brandId")
    public Integer updateByKey(BaseBrand brand) {
        clearCacheByBrandId(brand.getBrandId());
        clearCacheAll();
        return baseBrandDao.updateByKey(brand);
    }

    @Override
    @CacheEvict(cacheNames = "redis1d", key = "'brand:all'")
    public Integer insert(BaseBrand brand) {
        clearCacheByBrandId(brand.getBrandId());
        clearCacheAll();
        return baseBrandDao.insert(brand);
    }

    @Override
    public Integer deleteByKey(Integer brandId) {
        clearCacheByBrandId(brandId);
        clearCacheAll();
        return baseBrandDao.deleteByKey(brandId);
    }

    private void clearCacheByBrandId(Integer brandId) {
        commonCache.delByKey(CommonCache.PRERIX + "brand:id:" + brandId);
        commonCache.delByKey(CommonCache.PRERIX + "dubbo:list:brand:id:" + brandId);
    }

    private void clearCacheAll() {
        commonCache.delByKey(CommonCache.PRERIX + "brand:all");
        commonCache.delByKey(CommonCache.PRERIX + "dubbo:list:brands:all:");
        commonCache.delByKeyPrefix(CommonCache.PRERIX + "dubbo:list:brand:pageno");
    }

}
